<template>
  <div class="content">
    <div class="minContent" ref="echarts" @dblclick="dblClick"></div>
  </div>
</template>
<script>
import axios from "axios";
import { getProvinceMapInfo } from "@/utils/map_utils";
export default {
  data() {
    return {
      chartInstance: null,
      data: [],
    };
  },
  mounted() {
    this.init();
    this.getData();
    window.addEventListener("resize", this.widthChange);
    this.widthChange();
  },
  destroyed() {
    window.removeEventListener("resize", this.widthChange);
  },
  methods: {
    async init() {
      this.chartInstance = this.$echarts.init(this.$refs.echarts, "chalk");
      // http://localhost:8080/static/map/china.json
      const { data: res } = await axios.get(
        "http://localhost:8080/static/map/china.json"
      );
      console.log(res);
      this.$echarts.registerMap("china", res);
      const initOption = {
        title: {
          text: "▍ 商户分布与销量排行",
          textStyle: {
            fontWeight: "normal",
          },
          left: 20,
          top: 20,
        },
        legend: {
          left: "5%",
          bottom: "5%",
          orient: "vertical",
        },
        geo: {
          map: "china",
          top: "10%",
          bottom: "10%",
          itemStyle: {
            areaColor: "#2E72BF",
            borderColor: "#333",
          },
        },
      };
      this.chartInstance.setOption(initOption);
      this.chartInstance.on("click", this.mapClick);
    },
    async getData() {
      const { data: res } = await this.$http.get("map");
      console.log(res);
      this.data = res;
      this.updateEchart();
    },
    updateEchart() {
      let seriesData = this.data.map((v) => {
        return {
          type: "effectScatter",
          name: v.name,
          data: v.children,
          coordinateSystem: "geo",
          rippleEffect: {
            scale: 5,
            brushType: "stroke",
          },
        };
      });
      var dataOption = {
        series: seriesData,
      };
      this.chartInstance.setOption(dataOption);
    },
    widthChange() {
      //3. 自适应配置
      const titleSize = (this.$refs.echarts.offsetWidth / 100) * 3.6;
      const sizeOption = {
        title: {
          textStyle: {
            fontSize: titleSize,
          },
        },
        legend: {
          itemWidth: titleSize / 2,
          itemHeight: titleSize / 2,
          itemGap: titleSize / 2,
          textStyle: {
            fontSize: titleSize / 2,
          },
        },
      };
      this.chartInstance.setOption(sizeOption);
      this.chartInstance.resize();
    },
    async mapClick(e) {
      let areaInfo = getProvinceMapInfo(e.name);
      const { data: res } = await axios.get(
        "http://localhost:8080/" + areaInfo.path
      );

      this.$echarts.registerMap(areaInfo.key, res);
      const changeOption = {
        geo: {
          map: areaInfo.key,
        },
      };
      this.chartInstance.setOption(changeOption);
    },
    dblClick() {
      const chinaOption = {
        geo: {
          map: "china",
        },
      };
      this.chartInstance.setOption(chinaOption);
    },
  },
};
</script>